package com.zg.common.core.util;

import java.util.Random;
import java.util.UUID;

public class GUIDUtils {
    public static String getUUID32() {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        return uuid;
    }
    //获取唯一标识 并包含特定前缀
    public static String getUUID32WithPrefix(String Prefix) {
        String uuid32 = getUUID32();
        Random random = new Random();
        char[] Prefixc = Prefix.toCharArray();
        char[] chars = uuid32.toCharArray();

        //把前缀插入到uuid前面 并且 随机打乱 尽量减少重复的可能性
        for (int i = 0; i < Prefixc.length; i++) {
            char oc =  chars[i];
            chars[i] = Prefixc[i];
            //随机插入的位置
            int index = random.nextInt(32 - Prefixc.length)+Prefixc.length;
            chars[index] = oc;
        }


        return new String(chars);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            String uuid32 = getUUID32WithPrefix("FI");
            System.out.println(uuid32);
            System.out.println(uuid32.length());
        }
    }
}
